package com.genesis.yunnanji.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.activity.StoreWeb;
import com.genesis.yunnanji.bean.ShopPicBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.GenesisUtils;
import com.genesis.yunnanji.utils.NetUtils;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

import static com.genesis.yunnanji.config.GenesisApiConfig.PIC_HOST;

/**
 * Created by Administrator on 2018/4/25.
 */

public class Market extends Fragment {
    private View view;
    private GenesisUtils genesisUtils;
    private ImageView imageView;
    private Button button;

    public static Market newInstance(String param) {
        Market obj = new Market();
        Bundle bundle = new Bundle();
        bundle.putString("args1", param);
        obj.setArguments(bundle);
        return obj;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_mall, null);
            genesisUtils = GenesisUtils.getIntance();
            button = (Button) view.findViewById(R.id.bt_enter_mall);
            imageView = (ImageView) view.findViewById(R.id.iv_mall_iv);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), StoreWeb.class);
                    if (genesisUtils.isLogin()) {
                        intent.putExtra("url", GenesisApiConfig.HOST + "/Mall/Index/index.html" + GenesisApiConfig.FROM_APP + GenesisApiConfig.APP_TOKEN + genesisUtils.getAppToken());
                        startActivity(intent);
                    } else {
                        intent.putExtra("url", GenesisApiConfig.HOST + "/Mall/Index/index.html" + GenesisApiConfig.FROM_APP);
                        startActivity(intent);
                    }
                }
            });
            if (NetUtils.hasNetWorkConection(getActivity())) {
                getNetPic();
            }
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        return view;
    }

    private void getNetPic() {
        OkHttpUtils.get()
                .url(GenesisApiConfig.HOST + GenesisApiConfig.SHOP_PICTURE)
                .addParams("code", "app_index_mall")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        final ShopPicBean bean = gson.fromJson(response, ShopPicBean.class);
                        if (bean.status == 200) {
//                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
//                                View decorView = getActivity().getWindow().getDecorView();
//                                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//                                decorView.setSystemUiVisibility(option);
//                                getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
//                            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
//                                WindowManager.LayoutParams localLayoutParams = getActivity().getWindow().getAttributes();
//                                localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
//                            }
//                            StatusBarUtil.setColor(getActivity(), getResources().getColor(R.color.light), 0);
                            RequestOptions requestOptions = new RequestOptions()
                                    .error(R.mipmap.tmallpagesforandroid);
                            Glide.with(getActivity())
                                    .load(PIC_HOST + bean.result.list.get(0).advert_img)
                                    .apply(requestOptions)
                                    .into(imageView);
                            button.setVisibility(View.GONE);
                            imageView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(getActivity(), StoreWeb.class);
                                    intent.putExtra("url", bean.result.list.get(0).target_link);
                                    startActivity(intent);
                                }
                            });
                        }
                    }
                });
    }

}
